﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
/*
 *  2012.12.10
 *  作者：王述成
 *  DictTool 代码
 */
namespace OnTheWay.Tool
{
    public class XmlHelper : XmlDocument
    {
        #region 属性字段

        #region 静态属性
        /// <summary>
        /// xml版本号
        /// </summary>
        protected string sVersionCode = "1.0";
        /// <summary>
        /// xml编码
        /// </summary>
        protected string sEncipher = "gb2312";
        /// <summary>
        /// xmlstandalone
        /// </summary>
        protected string sStandalone = "yes";
        /// <summary>
        /// xml 跟元素名称
        /// </summary>
        protected string sRootName = "XmlDocument";
        /// <summary>
        /// xml 字典对象名称
        /// </summary>
        //protected static string sDictItemName = typeof(DictItem).Name;
        /// <summary>
        /// xml 字典对象集合名称
        /// </summary>
        //protected static string sDictItemsName = typeof(DictItem).Name + "s";

        
        #endregion

        #region 成员属性
        /// <summary>
        /// xml文件路径
        /// </summary>
        protected String mXmlFile;
        /// <summary>
        /// xml根节点
        /// </summary>
        protected XmlElement mRootNode;

        #endregion

        #endregion

        #region 构造方法
        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="xml">xml文件路径</param>
        internal XmlHelper(string xml)
        {
            mXmlFile = xml;
            this.InitizedXml();
        }
        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="xml">xml文件路径</param>
        internal XmlHelper(string xml,string rootname)
        {
            mXmlFile = xml;
            sRootName = rootname;
            this.InitizedXml();
        }
        #endregion

        #region 内部方法
        /// <summary>
        /// 初始化xml文档 创建或者加载
        /// </summary>
        /// <returns></returns>
        protected void InitizedXml()
        {
            try
            {
                this.Load(mXmlFile);
                mRootNode = this.LastChild as XmlElement;
            }
            catch //(Exception ex) 文件不存在时加载错误 创建文档
            {
                //创建根节点
                mRootNode = this.CreateElement(sRootName);
                //添加到文档并保存
                this.AppendChild(this.CreateXmlDeclaration(sVersionCode, sEncipher,sStandalone));
                this.AppendChild(mRootNode);
                this.Save(mXmlFile);
            }
        }
        /// <summary>
        /// 绑定元素到节点
        /// </summary>
        /// <param name="aXmlElement"></param>
        /// <param name="obj"></param>
        protected void BindingElement(ref XmlElement aXmlElement,Object obj)
        {
            Type tType = obj.GetType();
            foreach (PropertyInfo info in tType.GetProperties())
            {
                Object tobj = tType.InvokeMember(info.Name, BindingFlags.GetProperty, null, obj, null);
                tobj = (tobj == null) ? "null" : tobj;
                aXmlElement.SetAttribute(info.Name, tobj.ToString());
            }
        }

        #endregion

        #region 开放接口
        //
        // 摘要:
        //     将 XML 文档保存到文件。
        //
        // 异常:
        //   System.Xml.XmlException:
        //     此操作不产生格式良好的 XML 文档（例如，没有文档元素或重复的 XML 声明）。
        public virtual void Save()
        {
            this.Save(mXmlFile);
        }

        #endregion

    }
}
